﻿using ASmile.Com.Search;
using ASmile.DBModel;
using ASmile.Extend;
using ASmile.Helpers;
using ASmile.ORM;
using System;
using System.Collections.Generic;

namespace ASmile.Com.Da
{
    public class SearchBLL : BLLBase, ISearchBLL
    {
        public SearchBLL()
        { }
        /// <summary>
        /// 获取列信息
        /// </summary>
        public ApiResultList<Sys_GridColumn> GetColumns(GridView.ArgGetList arg)
        {
            return CreateBLL<GridViewBLL>().GetColumnInfo(arg);
        }
        /// <summary>
        /// 获取默认记录
        /// </summary>
        public ApiResult<Sys_ADRecord> GetDefRecord(ArgRecordSearch arg)
        {
            var query = DB.CreateQuery<Sys_ADRecord>();
            query.WhereAnd(s => s.FormName == arg.FormName && s.DgvName == arg.DgvName);
            query.WhereAnd(s => s.UserId == CurrUserId);
            query.OrderBy(s => s.LastUseTime, EmQueryOrder.Desc);
            var r = DB.Query<Sys_ADRecord>(query).ToEntity();
            return RetOK(r);
        }
        /// <summary>
        /// 指定记录Id更新缺省记录，用于无修改的搜索记录
        /// </summary>
        public ApiResult SaveDefRecord(ArgSaveRecord arg)
        {
            try
            {
                DB.TransStart(System.Data.IsolationLevel.ReadUncommitted);

                Sys_ADRecord t = new Sys_ADRecord();
                t.Id = arg.RecordId;
                t.LastUseTime = DateTime.Now;
                DB.Update(t);

                DB.Delete<Sys_ADRecord_Detail>(s => s.RecordId == arg.RecordId);

                for (int i = 0; i < arg.Details.Count; i++)
                {
                    var d = arg.Details[i];
                    d.OrderNo = i + 1;
                    DB.Insert(d);
                }
                DB.TransCommit();
                return ApiResult.RetOK("保存缺省记录成功");
            }
            catch (Exception ex)
            {
                DB.TransRollback();
                //LogHelper.LogObj.Error("保存高级查询记录时异常", ex);
                throw ex;
            }
        }
        /// <summary>
        /// 保存新的记录
        /// </summary>
        public ApiResult<string> SaveRecord(ArgSaveRecord arg)
        {
            try
            {
                DB.TransStart(System.Data.IsolationLevel.ReadUncommitted);
                var record = arg.Record;
                var details = arg.Details;
                if (record.Id.IsEmpty())
                {
                    record.Id = GuidHelper.NewId();
                    record.UserId = CurrUserId;
                    DB.Insert(record);
                }
                else
                {
                    record.LastUseTime = DateTime.Now;
                    DB.Update(record);
                }

                DB.Delete<Sys_ADRecord_Detail>(s => s.RecordId == record.Id);

                for (int i = 0; i < details.Count; i++)
                {
                    var d = details[i];
                    d.RecordId = record.Id;
                    d.OrderNo = i;
                    DB.Insert(d);
                }
                DB.TransCommit();
                return ApiResult.RetOK("", "保存成功");
            }
            catch (Exception ex)
            {
                DB.TransRollback();
                throw ex;
            }
        }
        /// <summary>
        /// 获取搜索记录条件明细
        /// </summary>
        public ApiResultList<Sys_ADRecord_Detail> GetRecordDetail(ArgMainId arg)
        {
            var recordId = arg.MainId;
            if (recordId.IsEmpty()) return null;
            var query = DB.CreateQuery<Sys_ADRecord_Detail>();
            query.WhereAnd(s => s.RecordId == recordId);
            query.OrderBy(s => s.OrderNo);
            var rList = DB.Query(query).ToList();
            return RetOK(rList, query.Total);
        }
        /// <summary>
        /// 获取指定的所有保存的记录
        /// </summary>
        public ApiResultList<Sys_ADRecord> GetRecordList(ArgRecordSearch arg)
        {
            var query = DB.CreateQuery<Sys_ADRecord>();
            query.WhereAnd(s => s.FormName == arg.FormName && s.DgvName == arg.DgvName);
            query.WhereAnd(s => s.UserId == CurrUserId);
            if (arg.SearchVal.IsNotEmpty())
            {
                string likeStr = $"%{arg.SearchVal}%";
                query.WhereAnd(s =>
                    s.WEx_Like(s.Caption, likeStr) ||
                    s.WEx_Like(s.CaptionCode, likeStr)
                );
            }
            query.OrderBy(s => s.LastUseTime, EmQueryOrder.Desc);
            var rList = DB.Query(query).ToList();
            return RetOK(rList, query.Total);
        }
        /// <summary>
        /// 删除搜索记录及明细
        /// </summary>
        public ApiResult DelRecord(Sys_ADRecord record)
        {
            DB.Delete<Sys_ADRecord>(s => s.Id == record.Id);
            DB.Delete<Sys_ADRecord_Detail>(s => s.RecordId == record.Id);
            return ApiResult.RetOK("删除成功");
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        public ApiResult<string> UpdateRecord(Sys_ADRecord record)
        {
            DB.Update(record);
            return ApiResult.RetOK("", "更新成功");
        }
    }
}